<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">主人账号</label>
                <div class="layui-input-block">
                    <input type="text" name="MASTER" lay-verify="numbers" lay-reqtext="master账号不能为空" placeholder="请输入master账号" value="" class="layui-input">
                    <tip>设置作为主人的QQ账号</tip>
                </div>
            </div>
    
                <div class="layui-form-item">
                    <label class="layui-form-label required">HOST</label>
                    <div class="layui-input-block">
                        <input type="text" name="HOST" lay-verify="required" lay-reqtext="HOST不能为空" placeholder="请输入HOST"  value="" class="layui-input">
                        <tip>127.0.0.1设置为本地运行外部无法访问, 修改后重启程序生效</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">使用端口</label>
                    <div class="layui-input-block">
                        <input type="text" name="PORT" lay-verify="numbers" lay-reqtext="运行端口号不能为空" placeholder="请输入运行端口号"  value="" class="layui-input">
                        <tip>本程序使用的端口号, 修改后重启程序生效</tip>
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">关联端口</label>
                <div class="layui-input-block" >
                    <input type="text" name="SENDPORT" lay-verify="numbers" lay-reqtext="关联端口不能为空" placeholder="请输入关联端口"  value="" class="layui-input">
                    <tip>go-cqhttp程序使用的端口号</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">计时器间隔/s</label>
                <div class="layui-input-block">
                    <input type="text" name="INTERVAL" lay-verify="numbers" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">数据库连接</label>
                <div class="layui-input-block">
                    <input type="text" name="CONNECTION" lay-verify="required" lay-reqtext="数据库连接不能为空" placeholder="请输入数据库连接" value="" class="layui-input">
                    <tip>sqlalchemy数据库连接, 修改后重启程序生效</tip> 
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">外部访问地址</label>
                <div class="layui-input-block">
                    <input type="text" name="OUT_ADDRESS"  value="" class="layui-input">
                    <tip>内网穿透或网站域名,带http/https协议</tip>
                </div>
            </div>

            
            <div class="layui-form-item">
                <label class="layui-form-label required">静态文件路径</label>
                <div class="layui-input-block">
                    <input type="text" disabled name="STATIC_PATH" lay-verify="required" lay-reqtext="静态文件路径不能为空" placeholder="请输入静态文件路径" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">上传文件路径</label>
                <div class="layui-input-block">
                    <input type="text" disabled name="UPLOADDIR" lay-verify="required" lay-reqtext="上传文件路径不能为空" placeholder="请输入上传文件路径" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">表单文件夹</label>
                <div class="layui-input-block">
                    <input type="text" disabled name="FORMDIR" lay-verify="required" lay-reqtext="表单路径不能为空" placeholder="请输入表单路径" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">表单模板</label>
                <div class="layui-input-block">
                    <input type="text" disabled name="FORMPROTOTYPE" lay-verify="required" lay-reqtext="表单模板不能为空" placeholder="请输入表单模板" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">表单提交文件</label>
                <div class="layui-input-block">
                    <input type="text" disabled name="FORM_UPLOAD_FILES_DIR" lay-verify="required" lay-reqtext="表单提交文件不能为空" placeholder="请输入表单提交文件" value="" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="setting">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/axios.min.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.$;

        var settings;

        

        // 加载数据
        axios.defaults.withCredentials=true;
        axios.post("/get_settings")
        .then(function (response) {
            console.log("response",response);
            // console.log("cookies",document.cookie);
            var data = response.data
            settings = data;

            //
            for (var k in data){
                var e = $("input[name="+k+"]")
                if (e) {
                    e.val(data[k]);
                }
            }
            
        }).catch(function (error) {
            layer.msg("数据加载失败!",{icon:2})
            console.log("err",error);
            return null;
        }); 

        //监听提交
        form.on('submit(setting)', function (data) {
            var postData  = {};
            for (var k in data.field){
                if (data.field[k] != settings[k]){
                    if (typeof settings[k] === "number"){
                        postData[k] = parseInt(data.field[k]);
                        settings[k] = parseInt(data.field[k]);
                    } else {
                        postData[k] = data.field[k];
                        settings[k] = data.field[k];
                    }
                }
            }
            if (JSON.stringify(postData) == "{}"){
                layer.msg("设置没有变更")
                return false;
            }
            var index = parent.layer.alert(JSON.stringify(postData), {
                title: '最终的提交信息'
            }, function() {
                    
                    axios.defaults.withCredentials=true;
                    axios.post("/update_settings", postData)
                    .then(function (response) {
                        console.log("response",response);
                        var data = response.data
                        layer.msg("数据修改成功!",{icon:1})

                    }).catch(function (error) {
                        layer.msg("数据修改失败",{icon:2})
                        console.log("err",error);
                        return null;
                    }); 
                    parent.layer.close(index);

            });
            return false;
        });

    });
</script>
</body>
</html>